#include "drv_common_def.h"
#include "dev_common.h"

ABS_NO_INLINE
void DelayCount(const uint32_t cnt)
{
    ABS_ASM (
        "mov r1, %0\n\t"
        "loop:\n\t"
        "nop\n\t"
        "subs r1, r1, #1\n\t"
        "bne loop\n\t"
        :
        : "r" (cnt)
        : "r1"
    );
}

static uint32_t g_msCnt = 0;
void Dev_Delay_init(void)
{
    volatile uint32_t n = 720000;
    uint32_t t1 = HAL_GetTick();
    DelayCount(n);
    uint32_t t2 = HAL_GetTick();
    uint32_t diff = t2 -t1;
    if (diff == 0) {
        diff = 10;
    }
    g_msCnt = 720000 / diff;
}

void Delay_us(uint32_t us)
{
    uint32_t cnt = (uint64_t)g_msCnt * us / 1000;
    DelayCount(cnt);
}

void Delay_ms(uint32_t ms)
{
    HAL_Delay(ms);
}